<template>
  <div class="login">
    <div class="login-box">
      <div class="avator-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login_form">
        <el-form
          ref="loginFormRef"
          label-width="0px"
          :model="form"
          :rules="rulesForm"
        >
          <!-- 登录 -->
          <el-form-item prop="username" >
            <el-input
              prefix-icon="iconfont icon-yonghu"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              prefix-icon="iconfont icon-suo"
              v-model="form.password"
              type="password"
            ></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login('loginFormRef')"
              >登录</el-button
            >
            <el-button @click="resetLoginForm('loginFormRef')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/axios/axios.js'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      rulesForm: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //重置
    resetLoginForm(loginFormRef) {
      this.$refs[loginFormRef].resetFields()
    },
    //登录
    login(loginFormRef) {
      this.$refs[loginFormRef].validate(async (valid) => {
        if (valid) {
          const { data } = await axios.post('/login', this.form)
          console.log(data)
          if (data.meta.status !== 200) {
            this.$message.error(data.meta.msg)
            return
          }
          this.$message.success(data.meta.msg)
          sessionStorage.setItem('TOKEN', data.data.token)
          this.$router.push('/')
        }
      })
    },
  },
  mounted(){
    console.log(this);
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-color: #2b4b6b;

  .login-box {
    width: 500px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .avator-box {
      width: 150px;
      height: 150px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 20px;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>